<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆编号" prop="vehicleNumber">
        <el-input
          v-model="queryParams.vehicleNumber"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="status" style="width: 300px;">
        <el-select v-model="queryParams.status" placeholder="请选择车辆状态" clearable>
          <el-option
            v-for="dict in vehicle_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="领养人名称" prop="nickName" label-width="90" v-hasPermi="['showNickName']" style="width: 300px;">
        <el-select v-model="queryParams.nickName" placeholder="请选择领养人名称" clearable>
          <el-option v-for="item in lyrList" :key="item.nickName" :value="item.nickName"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="日期" prop="vehicleDateRange">
        <el-date-picker clearable
          v-model="queryParams.vehicleDateRange"
          type="daterange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['project:vehicle:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column label="日期" align="center" prop="vehicleDate" />
      <el-table-column label="车辆编号" align="center" prop="vehicleNumber" />
      <el-table-column label="车辆状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="vehicle_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="领养人" align="center" prop="nickName" v-hasPermi="['showNickName']" />
<!--      <el-table-column label="归属人用户ID" align="center" prop="userId" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['project:vehicle:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['project:vehicle:remove']">删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="vehicleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车辆编号(如车牌号)" prop="vehicleNumber">
          <el-input v-model="form.vehicleNumber" placeholder="请输入车辆编号(如车牌号)" />
        </el-form-item>
        <el-form-item label="车辆状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in vehicle_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Vehicle">
import { listVehicle, getVehicle, delVehicle, addVehicle, updateVehicle } from "@/api/project/vehicle"
import {lyrGetlist} from "@/api/system/user.js";

const { proxy } = getCurrentInstance()
const { vehicle_status } = proxy.useDict('vehicle_status')

const vehicleList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    vehicleNumber: null,
    status: null,
    userId: null,
    createdAt: null,
    updatedAt: null,
    vehicleDateRange: []
  },
  rules: {
    vehicleNumber: [
      { required: true, message: "车辆编号(如车牌号)不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "车辆状态(0未激活,1空闲,2使用中,3维修中,4已停用)不能为空", trigger: "change" }
    ],
    userId: [
      { required: true, message: "归属人用户ID不能为空", trigger: "blur" }
    ],
    createdAt: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)
const lyrList = ref([])
/** 查询车辆信息列表 */
function getList() {
  loading.value = true
  listVehicle(queryParams.value).then(response => {
    vehicleList.value = response.rows
    total.value = response.total
    loading.value = false
  })

  lyrGetlist({params:{isPage:false}})
      .then(res => {
        console.log(res)
        lyrList.value = res.rows
      })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    vehicleId: null,
    vehicleNumber: null,
    status: null,
    userId: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("vehicleRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.vehicleId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加车辆信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _vehicleId = row.vehicleId || ids.value
  getVehicle(_vehicleId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改车辆信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["vehicleRef"].validate(valid => {
    if (valid) {
      if (form.value.vehicleId != null) {
        updateVehicle(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addVehicle(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _vehicleIds = row.vehicleId || ids.value
  proxy.$modal.confirm('是否确认删除车辆信息编号为"' + _vehicleIds + '"的数据项？').then(function() {
    return delVehicle(_vehicleIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('project/vehicle/export', {
    ...queryParams.value
  }, `vehicle_${new Date().getTime()}.xlsx`)
}

getList()
</script>
